package com.aims.server.controller;

import com.aims.api.dto.AccountDTO;
import com.aims.api.dto.request.AccountListRequest;
import com.aims.api.dto.response.AccountVO;
import com.aims.common.model.R;
import com.aims.server.facade.AccountFacade;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 账户控制器
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/api/account")
public class AccountController {

    @Autowired
    private AccountFacade accountFacade;

    /**
     * 获取账户列表
     */
    @GetMapping("/list")
    public R<Map<String, Object>> list(@Valid AccountListRequest request) {
        log.info("获取账户列表: {}", request);

        List<AccountVO> list = accountFacade.getAccountList(request);
        long total = accountFacade.getAccountCount(request);

        Map<String, Object> result = new HashMap<>();
        result.put("list", list);
        result.put("total", total);
        result.put("pageNum", request.getPageNum());
        result.put("pageSize", request.getPageSize());

        return R.success(result);
    }

    /**
     * 根据ID获取账户详情
     */
    @GetMapping("/{id}")
    public R<AccountVO> getById(@PathVariable Long id) {
        log.info("获取账户详情: {}", id);
        AccountVO account = accountFacade.getAccountById(id);
        return R.success(account);
    }

    /**
     * 创建账户
     */
    @PostMapping("/create")
    public R<Long> create(@Valid @RequestBody AccountDTO accountDTO) {
        log.info("创建账户: {}", accountDTO);
        Long id = accountFacade.createAccount(accountDTO);
        return R.success(id);
    }

    /**
     * 更新账户
     */
    @PutMapping("/update")
    public R<Void> update(@Valid @RequestBody AccountDTO accountDTO) {
        log.info("更新账户: {}", accountDTO);
        accountFacade.updateAccount(accountDTO);
        return R.success();
    }

    /**
     * 删除账户
     */
    @DeleteMapping("/{id}")
    public R<Void> delete(@PathVariable Long id) {
        log.info("删除账户: {}", id);
        accountFacade.deleteAccount(id);
        return R.success();
    }
}